<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Administrator_movie extends MY_Administrator {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();		
		//Load Model
		$this->load->model('administrator_movie_model');
    }

	/*Type Management*/
	public function type_management($module=NULL,$arg=NULL)
	{
		switch($module):
			case 'add':		
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_movie_model->type_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;
				
				$this->data['content'] = "administrator/movie/type_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);	
				//Check Id exist
				$is_exist = $this->db->from('movie_type')
				->where(array('movie_type_id' => $id))
				->count_all_results();
				if(empty($is_exist)) show_error('This id is not information.');	
								
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_movie_model->type_management_edit($id);
					redirect($this->session->userdata('returnUrl'));			
				endif;							

				$this->data['dataResult'] = $this->db->get_where('movie_type as t',array('t.movie_type_id' => $id),1)->row();
	
				$this->data['content'] = "administrator/movie/type_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;			
			case 'delete':
				$this->administrator_movie_model->type_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;												
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
					
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "movie_type_id DESC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case 'create_date':
						case 'update_date':
							$orderCondition = "t.".$orderby." ".$sort;
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
				
				
				/*Search Condition*/
				$searchCondition = array('t.movie_type_id IS NOT NULL');
				$movie_type_id = $this->input->get('movie_type_id');
				$movie_type_name = $this->input->get('movie_type_name');
				$update_by = $this->input->get('update_by');
				if(!empty($movie_type_id)) $searchCondition[] = "t.movie_type_id LIKE '%".$movie_type_id."%'";
				if(!empty($movie_type_name)) $searchCondition[] = "t.movie_type_name LIKE '%".$movie_type_name."%'";
				if(!empty($update_by)) $searchCondition[] = "t.update_by LIKE '%".$update_by."%'";
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?'.query_string(array('per_page'));
				$config['total_rows'] = $this->db->from('movie_type as t')->where(''.implode(' AND ',$searchCondition).'')->count_all_results();
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->order_by($orderCondition)->get_where('movie_type as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();
				
				$this->data['content'] = "administrator/movie/type_management";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}
	
	/*Hilight*/
	public function hilight($module=NULL,$arg=NULL)
	{
		//POST
		if(isset($_POST) && !empty($_POST)):
			$this->administrator_movie_model->hilight_update();
			redirect($this->session->userdata('returnUrl'));
		endif;		

		/*Keep Url*/
		$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));

		$this->data['dataResult'] = json_decode($this->db->get_where('module_page',array('page' => 'movie'),1)->row()->json);
						
		$this->data['content'] = 'administrator/movie/hilight';
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);
	}		
		
	/*Content Management*/
	public function content_management($module=NULL,$arg=NULL)
	{
		//Load ColorBox
		$this->data['js'][] = "jquery.colorbox-min.js";	
		$this->data['css'][] = "colorbox.css";
					
		switch($module):
			case 'add':		
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_movie_model->content_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;

				//Load Ckeditor
				$this->load->helper('ckeditor');
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";				
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";									
				
				$this->data['dataMovieType'] = $this->db->order_by('movie_type_name','ASC')->get('movie_type')->result();
				$this->data['dataMediaType'] = $this->db->order_by('media_type_name','ASC')->get('media_type')->result();
				$this->data['dataGenre'] = $this->db->order_by('genre_name','ASC')->get('genre')->result();
				
				$this->data['content'] = "administrator/movie/content_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);	
				//Check Id exist
				$is_exist = $this->db->from('movie')
				->where(array('movie_id' => $id))
				->count_all_results();
				if(empty($is_exist)) show_error('This id is not information.');	
									
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_movie_model->content_management_edit($id);
					redirect($this->session->userdata('returnUrl'));			
				endif;
				
				//Load Ckeditor
				$this->load->helper('ckeditor');
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";			
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";
				//Jquery Ui
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";				
				//Load ZeroClipboard
				$this->data['js'][] = "ZeroClipboard/ZeroClipboard.js";
				
				$this->data['dataMovieType'] = $this->db->order_by('movie_type_name','ASC')->get('movie_type')->result();
				$this->data['dataMediaType'] = $this->db->order_by('media_type_name','ASC')->get('media_type')->result();
				$this->data['dataMovieImage'] = $this->db->order_by('movie_image_order ASC,movie_image_id ASC')->get_where('movie_image',array('movie_id' => $id))->result();
				$dataMovieTypeRelate = $this->db->select('movie_type_id')->get_where('movie_type_relate',array('movie_id' => $id))->result();
				$this->data['dataMovieTypeRelate'] = array();
				if(!empty($dataMovieTypeRelate)) foreach($dataMovieTypeRelate as $value):
					$this->data['dataMovieTypeRelate'][] = $value->movie_type_id;
				endforeach;
							
				$this->data['dataResult'] = $this->db
				->join('movie_info as j','j.movie_id=t.movie_id','INNER')
				->get_where('movie as t',array('t.movie_id' => $id),1)
				->row();
	
				$this->data['content'] = "administrator/movie/content_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;
			case 'ajax_update_image':
				$this->administrator_movie_model->ajax_update_image();
			break;
			case 'ajax_delete_image':
				$id = intval($arg);
				$this->administrator_movie_model->ajax_delete_image($id);
			break;	
			case 'ajax_delete_media':
				$id = intval($arg);
				$field = $this->input->get('field');
				$this->administrator_movie_model->ajax_delete_media($id,$field);
			break;						
			case 'delete':
				$this->administrator_movie_model->content_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;												
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
					
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "movie_id DESC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case 'create_date':
						case 'update_date':
							$orderCondition = "t.".$orderby." ".$sort;
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
					
				/*Search Condition*/
				$searchCondition = array('t.movie_id IS NOT NULL');
				$movie_id = $this->input->get('movie_id');
				$is_video_clip = $this->input->get('is_video_clip');
				$movie_type_id = $this->input->get('movie_type_id');
				$media_type_id = $this->input->get('media_type_id');
				$movie_name = addslashes($this->input->get('movie_name'));
				$movie_desc = addslashes($this->input->get('movie_desc'));
				$update_by = addslashes($this->input->get('update_by'));
				if(!empty($movie_id)) $searchCondition[] = "t.movie_id LIKE '%".$movie_id."%'";
				if(isset($is_video_clip) && $is_video_clip!='') $searchCondition[] = "t.is_video_clip = '".$is_video_clip."'";
				if(!empty($movie_type_id)) $searchCondition[] = "t.movie_type_id = '".$movie_type_id."'";
				if(!empty($media_type_id)) $searchCondition[] = "t.media_type_id = '".$media_type_id."'";
				if(!empty($movie_name)) $searchCondition[] = "j.movie_name LIKE '%".$movie_name."%'";
				if(!empty($movie_desc)) $searchCondition[] = "j.movie_desc LIKE '%".$movie_desc."%'";
				if(!empty($update_by)) $searchCondition[] = "t.update_by LIKE '%".$update_by."%'";
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?'.query_string(array('per_page'));
				$config['total_rows'] = $this->db->from('movie as t')
				->join('movie_info as j','j.movie_id=t.movie_id','INNER')
				->where(''.implode(' AND ',$searchCondition).'')
				->count_all_results();
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->select('t.*,j.movie_name,j.movie_description')
				->join('movie_info as j','j.movie_id=t.movie_id','LEFT')
				->order_by($orderCondition)
				->get_where('movie as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();
				
				$this->data['dataMediaType'] = $this->db->order_by('media_type_name','ASC')->get('media_type')->result();
				$this->data['dataMovieType'] = $this->db->order_by('movie_type_name','ASC')->get('movie_type')->result();
				
				$this->data['content'] = "administrator/movie/content_management";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}
}